﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class admin_ChangeJianDa : System.Web.UI.Page
{
   //Datacon db = new Datacon();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            InitData();
            

        }

    }
    public void InitData()
    {
        //int JianDaProblemID = int.Parse(Request["ID"].ToString());   //取出传递过来的试题编号
        //JudgeProblem judgeproblem = new JudgeProblem();             //创建判断题对象
        //if (judgeproblem.LoadData(JianDaProblemID))                  //如果取出题目信息，分别放在相应控件显示
        //{
        //    db.ecDropDownList(ddlCourse, "select * from Course where c_id=(select c_id from JudgeProblem where ID='" + JianDaProblemID + "' )", "c_name", "c_id");
        //    txtTitle.Text = judgeproblem.Title;
        //    SqlConnection cn = db.getcon();
        //    cn.Open();
        //    string sqlstr1 = "select Answer from JudgeProblem where ID='" + Request["ID"].ToString() + "'";
        //    SqlCommand cmd1 = new SqlCommand(sqlstr1, cn);
        //    Label1.Text = cmd1.ExecuteScalar().ToString();
        //}
        //else                //查询出错，给出提示
        //{
        //    Response.Write("<script>alert('加载数据错误！');</script>");
        //    Response.Write("<script>window.location.href='ChangePanDuan.aspx'</script>");
        //}
        int JianDaProblemID = int.Parse(Request["ID"].ToString());   //取出传递过来的试题编号
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            var r = (from q in ds.JianDaProblem
                     where q.ID == JianDaProblemID
                     select q).FirstOrDefault();
            ddlCourse.DataSource = from q in ds.Course
                                   where q.c_id == r.c_id
                                   select q;
            ddlCourse.DataTextField = "c_name";
            ddlCourse.DataValueField = "c_id";
            ddlCourse.DataBind();
            txtTitle.Text = r.Title;
            Label1.Text = r.Answer.ToString();
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        //if (Page.IsValid)
        //{
        //    JudgeProblem judgeproblem = new JudgeProblem();          //创建判断题对象
        //    judgeproblem.c_id = int.Parse(ddlCourse.SelectedValue);//为判断题对象各属性赋值
        //    judgeproblem.Title = txtTitle.Text;
        //    judgeproblem.Answer = bool.Parse(rblAnswer.SelectedValue);
        //    if (Request["ID"] != null)                                  //如果是修改题目信息
        //    {
        //        judgeproblem.ID = int.Parse(Request["ID"].ToString()); //取出试题主键
        //        if (judgeproblem.UpdateByProc(int.Parse(Request["ID"].ToString())))//调用修改试题方法修改试题
        //        {
        //            Response.Write("<script>alert('修改成功！');</script>");
        //        }
        //        else
        //        {
        //            Response.Write("<script>alert('修改失败！');</script>");
        //        }
        //    }

        //}
        if (Page.IsValid)
        {
           
            if (Request["ID"] != null)                                  //如果是修改题目信息
            {
                using (DataClassesDataContext ds = new DataClassesDataContext())
                {
                    int JianDaProblemID = int.Parse(Request["ID"].ToString());   //取出传递过来的试题编号
                    var r = (from q in ds.JianDaProblem
                             where q.ID == JianDaProblemID
                             select q).FirstOrDefault();
                    r.c_id = int.Parse(ddlCourse.SelectedValue);//为单选题对象各属性赋值
                    r.Title = txtTitle.Text;
                    //r.Answer =
                    
                    ds.SubmitChanges();

                    Response.Write("<script>alert('修改成功！');</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('修改失败！');</script>");
            }

        }
    }
}